<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../libs/vue.js"></script>
</head>

<body>
    <div id="app">
        <h1>{{msg}}</h1>
        <button @click="updateMsg">点击修改</button>
        <h1>{{obj}}</h1>
        <button @click="updateObj">点击修改obj</button>
    </div>
    <script>
        let vm = new Vue({
            el: '#app',
            data: {
                msg: 'hello vue',
                obj: {
                    id: 1,
                    name: '张三',
                    age: 20
                }
            },
            methods: {
                updateMsg() {
                    this.msg = '哈哈哈'
                },
                updateObj() {
                    this.obj.name = '李四'
                }
            },
            // 侦听器
            watch: {
                // 普通监听，监听不到对象的改变
                // 写法1
                // msg(newVal,oldVal){
                //     console.log('改变了')
                //     console.log(newVal)
                //     console.log(oldVal)
                // }
                // 写法2
                msg: {
                    handler(newVal, oldVal) {
                        console.log('改变了')
                        console.log(newVal)
                        console.log(oldVal)
                    }
                },
                // 深度监听
                obj: {
                    handler(newVal, oldVal) {
                        console.log('对象发生改变了')
                        // console.log(newVal)
                        // console.log(oldVal)
                    },
                    deep: true
                },
                // 单独监听某个属性
                'obj.name': {
                    handler(newVal, oldVal) {
                        console.log('对象名发生改变了')
                        console.log(newVal)
                        console.log(oldVal)
                    },
                }
            }
        })
    </script>
</body>

</html>